cap program drop iv_ctrls

program define iv_ctrls

syntax varlist, indvar(varlist) iv(varlist) title(string)

	foreach depvar in `varlist' {

		local title `title'
		local label: var label `depvar' 
		local iv_label: var label `iv' 
			
		xtreg `indvar' `iv' $ictrls_3l_varying, fe
			local F_stat_round_ctrls = round(e(F),.001)	

		xtivreg `depvar' (`indvar' = `iv') $ictrls_3l_varying , fe
			eststo ucdp_all_ctrls
				estadd local FE "Yes"
				estadd local time "N/A"
				estadd local ctrls "Yes"
				estadd local IV "`iv_label'"
				estadd local F_stat "`F_stat_round_ctrls'"		
		
		foreach n in 0 1 2 3 {
		
		xtreg `indvar' `iv' $ictrls_3l_varying if ucdp_`n'yrs==1, fe
		local F_stat_round_ctrls = round(e(F),.001)	

					
		xtivreg `depvar' (`indvar' = `iv') $ictrls_3l_varying if ucdp_`n'yrs==1, fe
				eststo ucdp_`n'yrs_ctrls
					estadd local FE "Yes"
					estadd local time "`n'"
					estadd local ctrls "Yes"
				    estadd local F_stat "`F_stat_round_ctrls'"		
		}
				
						
		esttab ucdp_all_ctrls ucdp_0yrs_ctrls ucdp_1yrs_ctrls ucdp_2yrs_ctrls ucdp_3yrs_ctrls using "output/`title'.tex", replace ///
			label booktabs b(3) se(3) eqlabels(none) br ///
			mgroups("`label'", pattern(1 0 0 0 0 0 0 0 0 0) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			nomtitles ///
			star(* 0.10 ** 0.05 *** 0.01) nonotes scalars("ctrls Controls" "FE Country FE" "time Years of peace" "IV IV" "F_stat First stage F") 
	
		}
	
end
		
	
